package com.fengpanlong.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.fengpanlong.entity.EmployeeProject;
import com.fengpanlong.util.DBHelp;
import com.fengpanlong.util.RowMapper;

public class EmployeeProjectDao {
	private DBHelp<EmployeeProject> db = new DBHelp<EmployeeProject>();
	
	private class EmployeeProjectRowMapper implements RowMapper<EmployeeProject> {

		public EmployeeProject mapperRow(ResultSet rs) throws SQLException {
			EmployeeProject e = new EmployeeProject();
			e.setEmployeeId(rs.getString("employeeid"));
			e.setProjectId(rs.getString("projectid"));
			e.setRole(rs.getString("role"));
			return e;
		}
		
	}
	/**
	 * 通过项目id和用户id得到关系对象
	 * @param employeeId  用户id
	 * @param projectId 项目id
	 * @return 关系对象
	 */
	public EmployeeProject findEPByEmployeeIdAndProjectId(String employeeId,
			String projectId) {
		String sql = "select employeeid,projectid,role from t_employee_project where employeeid = ? and projectid = ?";
		EmployeeProject ep = db.executeQueryForObject(sql, new EmployeeProjectRowMapper(), employeeId, projectId);
		if(ep != null) {
			EmployeeDao ed = new EmployeeDao();
			ep.setEmployee(ed.findById(employeeId));
			ProjectDao pd = new ProjectDao();
			ep.setProject(pd.findById(projectId));
		}
		return ep;
	}
}
